from celery import  Celery
from django.conf import  settings
from django.core.mail import  send_mail

import  os
import  django
import  time

os.environ.setdefault('DJANGO_SETTINGS_MODULE','dailyfresh.settings')
django.setup()

# 在Django项目初始化之后再进行导入
from django_redis import get_redis_connection

#创建一个实例对象
app = Celery('celery_tasks.tasks',broker='redis://127.0.0.1:6379/2')

# 定义任务函数,发送激活邮件
@app.task
def send_register_active_email(to_email,username,token):
    subject = '注册成功通知'  # 标题
    message = 'gook luck 恭喜您即将成为我们的一员'  # 邮件正文
    sender = settings.EMAIL_FROM  # 发件人
    receiver = [to_email]  # 收件人
    html_message = """
            <h1>%s,恭喜您,成为我们的会员,</h1><br/>
            <h3>请您在1小时之内点击以下链接进行账户激活</h3>
            <a href="http://127.0.0.1:8000/user/active/%s">http://127.0.0.1:8000/user/active/%s</a>
            """ % (username, token, token)
    send_mail(subject, message, sender, receiver, html_message=html_message)
    # 为了体现出celery异步完成发送邮件，这里睡眠5秒
    time.sleep(20)